import Mongodb from "mongodb";
const StoreConfig = require("../config/store.json");
let mongo: Mongodb.Db | null = null;
const NODE_ENV = process.env.NODE_ENV || "development";
const mongodbConfig = StoreConfig[NODE_ENV]["mongodb"];
export async function getMongo(): Promise<Mongodb.Db> {
  try {
    if (mongo) {
      return mongo;
    }
    const mongoClient = new Mongodb.MongoClient(mongodbConfig.url);
    const client = await mongoClient.connect();
    mongo = client.db(mongodbConfig.db);
    return mongo;
  } catch (error) {
    mongo = null;
    return await getMongo();
  }
}
